package com.feicheng.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.persistence.entity.CnRegionInfo;
import com.feicheng.persistence.service.ICnRegionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class FLocationServiceImpl {

    private final Integer R = 6371;

    @Autowired
    ICnRegionInfoService cnRegionInfoService;

    public double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double lonres = lon1 > lon2 ? lon1 - lon2 : lon2 - lon1;
        double latres = lat1 > lat2 ? lat1 - lat2 : lat2 - lat1;
        double dLat = this.toRadians(latres);
        double dLon = this.toRadians(lonres);

        double a1 = Math.sin(dLat / 2) * Math.sin(dLat / 2);
        double a2 = Math.cos(this.toRadians(lat1));
        double a3 = Math.cos(this.toRadians(lat2));

        double a = a1 +
                a2 * a3 *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return R * c * 1000;
    }

    private double toRadians(double d) {
        return d * Math.PI / 180;
    }


}
